package 
{
	import laya.display.Node;
	import laya.d3.core.material.StandardMaterial;
	import laya.d3.core.MeshSprite3D;
	import laya.d3.resource.models.Mesh;
	import laya.d3.core.Sprite3D;
	import laya.d3.math.Vector3;
	import laya.events.Event;
	import laya.d3.component.animation.SkinAnimations;
	import laya.ani.AnimationTemplet;
	import laya.d3.component.Animator;
	import laya.d3.animation.AnimationNode;
	import laya.d3.animation.AnimationClip;
	import laya.ani.AnimationState;
	import laya.d3.math.Quaternion;
	import laya.d3.math.Matrix4x4;
	import laya.ui.Button;

	/**
	 * ...
	 * @author
	 */
	public class Player extends Sprite3D{
		private var longbow:Sprite3D;
		private var animator:Animator;
		private var arrow:Sprite3D;
		private var curTime:Number;
		private var shotBtn:Button;

		private var engine:*;
		public function Player(){
			init();
		}

		private function init():void
		{
  			longbow = ModelManager.getInstance().getModel("Longbow_b");
			animator = longbow.getComponentByType(Animator) as Animator;
			animator.playOnWake = false;
			arrow = ModelManager.getInstance().getModel("Arrow");
			//默认不显示
			arrow.active = false;
			//一定要加入到场景才能挂载
			this.addChild(arrow);
			//挂载到骨骼节点
			animator.linkSprite3DToAvatarNode("string",arrow);

  			this.addChild(longbow);
			longbow.transform.rotate(new Vector3(0,180,0),true,false);
			longbow.transform.position = new Vector3(0.4,-0.2,-1);
		}

		public function bindShotBtn(btn:Button):void
		{
			this.shotBtn = btn;
			this.shotBtn.on(Event.MOUSE_DOWN, this, mouseDown);
			this.shotBtn.on(Event.MOUSE_UP, this, mouseUp);
		}

		private function mouseDown():void
		{
			animator.play("Draw");
			arrow.active = true;
			curTime = Laya.timer.currTimer;
		}

		private function mouseUp():void
		{
			animator.stop();
			var percent:Number = animator.currentFrameTime / animator.getClip("Draw").duration();
			//trace("currentFrameTime: " + animator.currentFrameTime + " endTime: " + animator.getClip("Draw").duration());
			var totalAnimationClip:AnimationClip = animator.getClip("Shoot");
			//动画总时长
			var maxTime:Number = totalAnimationClip.duration();
			//每帧间隔
			var frameInterval:int = 1 / totalAnimationClip._frameRate;
			//总帧数
			var endFrame:int = Math.floor(maxTime / frameInterval);
			//计算开始帧
			var startFrame:int = Math.floor(endFrame * (1 - percent));
			animator.addClip(totalAnimationClip,"runShoot",startFrame);
			animator.play("runShoot");
			animator.on(Event.STOPPED, this, this.removeClip);
			animator.on(Event.COMPLETE, this, this.removeClip);

			arrow.active = false;
			
			//创建一个新的箭
			var runArrow:Sprite3D = arrow.clone();
			runArrow.active = true;
			runArrow.transform.worldMatrix = arrow.transform.worldMatrix.clone();
			var time:Number = Laya.timer.currTimer - curTime;
			var timePer:Number = time / GameManager.drawTime;
			if(timePer > 1)
				timePer = 1;
			var velocity:Number = timePer * GameManager.speed;
			GameManager.getInstance().addArrow(runArrow,velocity);
		}

		private function removeClip():void
		{
			animator.removeClipByName("runShoot");
		}
	}

}